package com.nodity.gui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Stroke;
import java.awt.geom.Line2D;

public class NodeJoin extends Line2D.Double implements Selectable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public boolean selected;
	
	
	public NodeJoin(Line2D.Double l){
		setLine(l);
		
	}
	
	public NodeJoin(int x1, int y1, int x2, int y2) {
		setLine(x1,y1,x2,y2);
	}

	public void renderJoin(Graphics g){
		
		int x1,y1,x2,y2;
			x1=(int) getX1();
			y1=(int) getY1();
			x2=(int) getX2();
			y2=(int) getY2();
		
			if(selected){
			
			g.setColor(Color.RED);
			
			
			
			g.drawLine(x1,y1,x2,y2);
			g.setColor(Color.BLACK);
			g.fillRect(x1, y1-1, 4, 4);
			g.fillRect(x2, y2-1, 4, 4);
		
		}else{
			g.setColor(Color.BLACK);
			g.drawLine(x1,y1,x2,y2);
		}
	}



	@Override
	public boolean isSelected(Point p) {
		
		
		// make the line slightly thicker and test for intersection  where the mouse cursor is using a small rectangle
		int x1,y1,x2,y2;
		x1=(int) getX1()-1;
		y1=(int) getY1()-1;
		x2=(int) getX2()+1;
		y2=(int) getY2()+1;
		
		Rectangle r=new Rectangle(MouseListen.mouseCoords.x-5,MouseListen.mouseCoords.y-5,10,10);
		
		
		if(new Line2D.Double(x1,y1,x2,y2).intersects(r)){
			return true;
		}
		
		
		
	
		
		
		
		
		
		return false;
	}



	@Override
	public void setSelected(Point p) {
		if(isSelected(p)){
		selected=true;
	}else
		selected=false;

		
	}
	
}
